import {Model,ModelDefinition} from './Model';
// 修复静态 delete 方法返回类型不匹配的问题
export class ChatSession extends Model  {
    static fields: ModelDefinition = {
      id: {
        type: 'string',
        primaryKey: true,
        autoIncrement: true
      },
      userId: {
        type: 'string',
        allowNull: false,    
      },
      title: {
        type: 'string',
        allowNull: true
      },
      createdAt: {
        type: 'date',
        allowNull: false
      },
      lastActive: {
        type: 'date',
        allowNull: false
      },
      
    };
    static   getSessionByUserId(userId: string) {
      try {
        const result =   this.findAll({ userId });
        return result as ChatSession[];
      } catch (error) {
        console.error('Error getting sessions by userId:', error);
        throw error;
      }
    }

    static   updateTitle(sessionId: string, userId: string, title: string) {
      try {
        return   this.update({ title }, { id: sessionId, userId });
      } catch (error) {
        console.error('Error updating session title:', error);
        throw error;
      }
    }

    static   delete(conditions: { id: string; userId: string }) {
      try {
        const sql = `DELETE FROM ${this.tableName} WHERE id = ? AND userId = ?`;
        const stmt = this.db.prepare(sql);
        return stmt.run(conditions.id, conditions.userId);
      } catch (error) {
        console.error('Error deleting session:', error);
        throw error;
      }
    }
  }
 